<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入物料编号"></p-input>
        </p-form-item>
        <p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入物料名称"></p-input>
        </p-form-item>
        <p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'comment', validatorRules.comment]" placeholder="请输入物料描述"></p-input>
        </p-form-item>
        <p-form-item label="物料组" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <f-popup
            v-decorator="['groupId', validatorRules.groupId]"
            :trigger-change="true"
            org-fields="group_name"
            dest-fields="group_id"
            code="mat_group"
            @callback="popupCallback"/>
        </p-form-item>
        <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mat_status" placeholder="请选择状态"/>
        </p-form-item>
        <p-form-item label="替代物料标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <f-popup
            v-decorator="['substituteCode', validatorRules.substituteCode]"
            :trigger-change="true"
            org-fields="code"
            dest-fields="substitute_code"
            code="mat_substitute"
            @callback="popupCallback"/>
        </p-form-item>
        <p-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入创建人"></p-input>
        </p-form-item>
        <p-form-item label="损耗率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'attritionRate', validatorRules.attritionRate]" placeholder="请输入损耗率"></p-input>
        </p-form-item>
        <p-form-item label="预警值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'prewarningValue', validatorRules.prewarningValue]" placeholder="请输入预警值"></p-input>
        </p-form-item>
        <p-form-item label="关键物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['keyMaterial', validatorRules.keyMaterial]" :trigger-change="true" dictCode="mat_key_material" placeholder="请选择关键物料"/>
        </p-form-item>
        <p-form-item label="管控类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'controlType', validatorRules.controlType]" placeholder="请输入管控类型"></p-input>
        </p-form-item>
        <p-form-item label="物料类别码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'categoryCode', validatorRules.categoryCode]" placeholder="请输入物料类别码"></p-input>
        </p-form-item>
        <p-form-item label="物料有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'materialValidity', validatorRules.materialValidity]" placeholder="请输入物料有效期" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="生产类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'manufacturingType', validatorRules.manufacturingType]" placeholder="请输入生产类型"></p-input>
        </p-form-item>
        <p-form-item label="物料单位代号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'unitCode', validatorRules.unitCode]" placeholder="请输入物料单位代号"></p-input>
        </p-form-item>
        <p-form-item label="物料单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'materialUnit', validatorRules.materialUnit]" placeholder="请输入物料单位"></p-input>
        </p-form-item>
        <p-form-item label="最小单元体" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'minimumElement', validatorRules.minimumElement]" placeholder="请输入最小单元体"></p-input>
        </p-form-item>
        <p-form-item label="批数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'batchNumber', validatorRules.batchNumber]" placeholder="请输入批数量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择生效时间" v-decorator="[ 'effectiveTime', validatorRules.effectiveTime]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="失效时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择失效时间" v-decorator="[ 'deadTime', validatorRules.deadTime]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="更新人标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'updatetimeBy', validatorRules.updatetimeBy]" placeholder="请输入更新人标识"></p-input>
        </p-form-item>
        <p-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择更新时间" v-decorator="[ 'updatetimeTime', validatorRules.updatetimeTime]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择时间标识" v-decorator="[ 'timestamp', validatorRules.timestamp]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
        </p-form-item>
        <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
        </p-form-item>
        <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
        </p-form-item>
        <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
        </p-form-item>
        <p-form-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'company', validatorRules.company]" placeholder="请输入公司"></p-input>
        </p-form-item>
        <p-form-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'factory', validatorRules.factory]" placeholder="请输入工厂"></p-input>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MaterialMesModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
          ]},
          name: {rules: [
            {required: true, message: '请输入物料名称!'},
          ]},
          comment: {rules: [
            {required: true, message: '请输入物料描述!'},
          ]},
          groupId: {rules: [
          ]},
          status: {rules: [
          ]},
          substituteCode: {rules: [
          ]},
          createBy: {rules: [
          ]},
          attritionRate: {rules: [
          ]},
          prewarningValue: {rules: [
          ]},
          keyMaterial: {rules: [
          ]},
          controlType: {rules: [
          ]},
          categoryCode: {rules: [
          ]},
          materialValidity: {rules: [
          ]},
          manufacturingType: {rules: [
          ]},
          unitCode: {rules: [
          ]},
          materialUnit: {rules: [
          ]},
          minimumElement: {rules: [
          ]},
          batchNumber: {rules: [
          ]},
          effectiveTime: {rules: [
          ]},
          deadTime: {rules: [
          ]},
          updatetimeBy: {rules: [
          ]},
          updatetimeTime: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
          company: {rules: [
          ]},
          factory: {rules: [
          ]},
        },
        url: {
          add: '/basic-business/basicMes/materialMes/add',
          edit: '/basic-business/basicMes/materialMes/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','name','comment','groupId','status','substituteCode','createBy','attritionRate','prewarningValue','keyMaterial','controlType','categoryCode','materialValidity','manufacturingType','unitCode','materialUnit','minimumElement','batchNumber','effectiveTime','deadTime','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId','company','factory'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','comment','groupId','status','substituteCode','createBy','attritionRate','prewarningValue','keyMaterial','controlType','categoryCode','materialValidity','manufacturingType','unitCode','materialUnit','minimumElement','batchNumber','effectiveTime','deadTime','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId','company','factory'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>